#coding:utf-8
import os
import time
from common.koufuApi import KoufuApi

__author__ = 'yangxianming'

from common.KoufuConf import KoufuConf
import json
import requests


def case():
    #初始化必要的参数
    kou_fu_conf = KoufuConf()
    test_url = kou_fu_conf.koufu_prod_domain + "/api/koufu"
    kou_fu_api = KoufuApi()
    script_name = os.path.basename(__file__)
    tf = True
    test_result = ""

    try:
        #企业余额查询
        p = kou_fu_api.get_efuli_koufu_corp_combo_goods_list_balance_data()
        r = requests.post(test_url, data=json.dumps(p), headers=kou_fu_conf.headers)
        print("这个是接口efuli_koufu_corp_combo_goods_list_balance返回的全部参数\n", r.content.decode("utf-8"))
        before_balance = r.json()["return_data"]    #得到初始余额


        #获得商铺地址
        p = kou_fu_api.get_efuli_koufu_merchantshop_query_bycoordinates_data()
        r = requests.post(test_url, data=json.dumps(p), headers=kou_fu_conf.headers)
        print("这个是接口efuli_koufu_merchantshop_query_bycoordinates返回的全部参数\n", r.content.decode("utf-8"))
        mealRuleId = r.json()["return_data"]["mealRuleId"]

        #创建餐规
        mark = "每日餐补" + str(int(time.time() * 1000))
        p = kou_fu_api.get_efuli_koufu_mealrule_merge_data(update_body={"ruleName":mark,"sendDays":"ED","voucherAmount":100,"mealRuleId":mealRuleId})
        r = requests.post(test_url, data=json.dumps(p), headers=kou_fu_conf.headers)
        print("这个是接口efuli_koufu_mealrule_merge返回的全部参数\n", r.content.decode("utf-8"))

        #立即方放
        p = kou_fu_api.get_efuli_koufu_mealrule_send_data(update_body={"mealRuleId":mealRuleId})
        r = requests.post(test_url, data=json.dumps(p), headers=kou_fu_conf.headers)
        print("这个是接口efuli_koufu_mealrule_send返回的全部参数\n", r.content.decode("utf-8"))

        #企业余额查询
        p = kou_fu_api.get_efuli_koufu_corp_combo_goods_list_balance_data()
        r = requests.post(test_url, data=json.dumps(p), headers=kou_fu_conf.headers)
        print("这个是接口efuli_koufu_corp_combo_goods_list_balance返回的全部参数\n", r.content.decode("utf-8"))
        after_balance = r.json()["return_data"]    #得到发放后余额

        test_result = int(before_balance) - int(after_balance)
        print("验证发放后余额是否减少100", test_result)
        #具体判断
        if  test_result != 100:
            tf = False
    except Exception as e:
        err_info = str(e).replace("'", "").replace("\"", "")
        test_result = "非预期的异常,脚本文件:" + script_name + "; 错误信息：" + err_info
        tf = -2

    #定义预期描述
    print(str(tf))
    print(test_result)
    expect_result = "验证发放后余额是否减少100, 差值test_result=" + str(test_result)

    #返回测试结果 第一个参数:True or False    第二个参数测试结果描述
    return tf, [test_result, expect_result]

# case()